package cn.kgc.shiro02.service.impl;

import cn.kgc.shiro02.commons.CodeMsg;
import cn.kgc.shiro02.commons.ResponseResult;
import cn.kgc.shiro02.dto.RolePageDTO;
import cn.kgc.shiro02.entity.RolePers;
import cn.kgc.shiro02.entity.UserRole;
import cn.kgc.shiro02.mapper.RolePersMapper;
import cn.kgc.shiro02.mapper.UserRoleMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.shiro02.entity.Role;
import cn.kgc.shiro02.service.RoleService;
import cn.kgc.shiro02.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService{

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RolePersMapper rolePersMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public ResponseResult selectRolePage(RolePageDTO rolePageDTO) {

        Page<Role> rolePage = new Page<>(rolePageDTO.getPage(), rolePageDTO.getLimit());

        String roleName = rolePageDTO.getRoleName();

        LambdaQueryWrapper<Role> lambda = new QueryWrapper<Role>().lambda();
        lambda.eq((roleName!=null&&!"".equals(roleName)),Role::getRoleName,roleName);
        roleMapper.selectPage(rolePage, lambda);

        long total = rolePage.getTotal();
        List<Role> records = rolePage.getRecords();

        return new ResponseResult(CodeMsg.SUCCESS,total,records);
    }

    @Override
    public ResponseResult selectPermissionByRoleId(Integer roleId) {

        LambdaQueryWrapper<RolePers> lambda = new QueryWrapper<RolePers>().lambda();
        lambda.eq(RolePers::getRoleId,roleId);
        List<RolePers> rolePers = rolePersMapper.selectList(lambda);

        List<Integer> perIds = rolePers.stream().map(rp -> rp.getPerId()).collect(Collectors.toList());
        return new ResponseResult(CodeMsg.SUCCESS,null,perIds);
    }

    @Override
    public ResponseResult addRolePermission(Integer roleId, String permissionIds) {

        //1. 根据角色id  删除角色权限信息 -> 角色权限关联表
        LambdaQueryWrapper<RolePers> lambda = new QueryWrapper<RolePers>().lambda();
        lambda.eq(RolePers::getRoleId,roleId);
        rolePersMapper.delete(lambda);

        // 2.将角色最新的权限信息写入 角色权限关联表
        String[] ids = permissionIds.split(",");
        for (String id : ids) {
            RolePers rolePers = new RolePers();
            rolePers.setPerId(Integer.valueOf(id));
            rolePers.setRoleId(roleId);
            rolePersMapper.insert(rolePers);
        }

        return new ResponseResult(CodeMsg.SUCCESS);
    }


    public ResponseResult saveOrUpdateRole(Role entity) {

        //判定角色信息是否已存在  角色名 角色描述
        LambdaQueryWrapper<Role> lambda = new QueryWrapper<Role>().lambda();
        lambda.eq(Role::getComments,entity.getComments())
                .or()
                .eq(Role::getRoleName,entity.getRoleName());

        Role role = roleMapper.selectOne(lambda);
        if(role!=null){
           return   new ResponseResult(CodeMsg.ROLE_EXIST);
        }

        // 新增
        if(entity.getRoleId()==null){
            roleMapper.insert(entity);
        }else {   // 修改
            roleMapper.updateById(entity);
        }

        return new ResponseResult(CodeMsg.SUCCESS);
    }

    @Override
    public ResponseResult deleteByIds(String ids) {

        String[] strIds = ids.split(",");
        List<Integer> intIds = Arrays.asList(strIds).stream().map(id -> Integer.valueOf(id)).collect(Collectors.toList());

        // 1. 删除的角色是否被占用  如果被占用 禁止删除角色信息
        LambdaQueryWrapper<UserRole> lambda = new QueryWrapper<UserRole>().lambda();
        lambda.in(UserRole::getRoleId,intIds);
        List<UserRole> userRoles = userRoleMapper.selectList(lambda);

        if(userRoles.size()>0){
            return  new ResponseResult(CodeMsg.ROLE_HAS_USED);
        }

        //2. 删除角色信息   删除的角色是否具有权限信息
         roleMapper.deleteBatchIds(intIds);
         userRoleMapper.delete(lambda);
        // 3.删除角色关联的权限信息
        LambdaQueryWrapper<RolePers> lambda1 = new QueryWrapper<RolePers>().lambda();
        lambda1.in(RolePers::getRoleId,intIds);
        rolePersMapper.delete(lambda1);


        return new ResponseResult(CodeMsg.SUCCESS);
    }
}




